<template>
  <div class="container">
    <div class="video-detail">
      <div class="section">
        <video
          id="video"
          @timeupdate="onTimeUpdate"
          v-if="detail.ecas"
          class="video"
          :src="detail.ecas[currentIndex].resourceLinkPath"
          controls="controls"
        ></video>
        <div class="title">{{detail.title}}</div>
        <div class="hits">{{detail.browseCount||0}}次播放</div>
        <div class="btns">
          <div class="left">
            <span>
              <van-icon name="chat-o" color="#e71f19"/>
              <span>学习笔记</span>
            </span>
          </div>
          <div class="right">
            <span @click="setCollect">
              <van-icon name="star-o" color="#e71f19"/>
              <span>收藏</span>
            </span>
            <span>
              <van-icon name="share" color="#e71f19"/>
              <span>分享</span>
            </span>
          </div>
        </div>
      </div>
      <div class="section">
        <div class="title">选集</div>
        <div class="handlers">
          <div
            v-for="(item,index) in detail.ecas"
            :key="item.id"
            :class="['item',{'active':index===currentIndex}]"
            @click="onChangeCurrentIndex(index)"
          >{{index+1}}</div>
        </div>
      </div>
      <div class="section">
        <div class="title">视频推荐</div>
        <div class="list">
          <div
            class="video-cell"
            v-for="item in recommend"
            :key="item.id"
            @click="goDetail(item.id)"
          >
            <div class="left">
              <!-- <video class="video" :src="require('@/assets/video/ceshi.mp4')"></video> -->
              <img class="video" :src="item.firstImage|imgPath">
            </div>
            <div class="right">
              <div class="title">{{item.title}}</div>
              <div class="date">
                <div class="date-left">
                  <van-icon name="play-circle-o"/>
                  <span>{{item.countComment||0}}次播放量</span>
                </div>
                <div>已学习{{item.learnedProc||0}}%</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      id: '',
      currentIndex: 0,
      detail: {},
      recommend: []
    }
  },
  created () {
    const id = this.$route.params.id
    if (id) {
      this.id = id
      this.getDetail()
    }
    this.getRecommend()
    this.throttleAjax = this.throttle(e => {
      const { currentTime, duration } = e.srcElement
      this.reportProcess(currentTime, duration)
    }, 1000)
  },
  methods: {
    onTimeUpdate (e) {
      if (this.detail.learnedProc !== 100) {
        this.throttleAjax(e)
      }
    },
    onChangeCurrentIndex (index) {
      if (this.currentIndex !== index) {
        this.currentIndex = index
      }
    },
    goDetail (id) {
      this.$router.push({
        path: `/study-center/detail/video/${id}`
      })
    },
    getDetail () {
      this.$axios
        .get('/xbkj-dj/web/edu/course/detail', {
          id: this.id
        })
        .then(res => {
          console.log(res)
          this.detail = res.data
        })
    },
    setCollect () {
      this.$axios
        .get('/xbkj-dj/web/edu/course/collect', {id: this.id})
        .then(res => {
          console.log(res)
        })
    },
    getRecommend () {
      this.$axios
        .get('/xbkj-dj/web/edu/course/recommend', {
          type: 1
        })
        .then(res => {
          console.log(res)
          this.recommend = [res.data || []]
        })
    },
    reportProcess (learnedSecond, totalSecond) {
      const { id: resourceId } = this.detail.ecas[this.currentIndex]
      this.$axios.post('/xbkj-dj/web/edu/course/learn/report', {
        id: this.id,
        resourceId,
        totalSecond,
        learnedSecond
      })
    },
    throttle (fn, time) {
      let _self = fn
      let timer
      let firstTime = false // 记录是否是第一次执行的flag
      return function () {
        let args = arguments // 解决闭包传参问题
        let _me = this // 解决上下文丢失问题
        if (firstTime) {
          // 若是第一次，则直接执行
          _self.apply(_me, args)
          return (firstTime = false)
        }
        if (timer) {
          // 定时器存在，说明有事件监听器在执行，直接返回
          return false
        }
        timer = setTimeout(function () {
          clearTimeout(timer)
          timer = null
          _self.apply(_me, args)
        }, time || 500)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.video-detail {
  background-color: #fff;
  .section {
    border-bottom: 1px solid #d6d6d6;
    padding: 20px 20px 10px 20px;
  }
  .video {
    width: 100%;
    border-radius: 8px;
    margin-bottom: 10px;
  }
  .title {
    font-size: 15px;
    font-weight: bold;
    line-height: 1;
    margin-bottom: 10px;
  }
  .hits {
    font-size: 12px;
    color: #999;
    line-height: 1;
    margin-bottom: 10px;
  }
  .btns {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      > span {
        display: flex;
        align-items: center;
      }
    }
    .right {
      display: flex;
      > span {
        display: flex;
        align-items: center;
        margin-left: 20px;
      }
    }
  }
}
.handlers {
  display: flex;
  flex-wrap: wrap;
  .item {
    flex: 0 0 40px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    margin: 5px 5px;
    background-color: #f5f7f9;
    &.active {
      color: #e71f19;
    }
  }
}
.video-cell {
  display: flex;
  width: 100%;
  padding: 20px 0;
  border-bottom: 1px dashed #e5e5e5;
  background-color: #fff;
  .right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 5px 0;
    .title {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      /*! autoprefixer: off*/
      -webkit-box-orient: vertical;
      /* autoprefixer: on*/
      -webkit-line-clamp: 2;
      font-size: 14px;
    }
    .date {
      font-size: 12px;
      color: #999;
      display: flex;
      justify-content: space-between;
      .date-left > * {
        vertical-align: middle;
      }
    }
  }
  .left {
    flex: 0 0 115px;
    width: 115px;
    height: 75px;
    margin-right: 8px;
    video {
      display: block;
      width: 100%;
      height: 100%;
      border-radius: 6px;
    }
  }
}
</style>
